package com.java.comprehensive.other.sort;

import java.util.Arrays;

/**
 * @Author: Dgg
 * @Date: 2021/10/10 10:55
 */
public class Selection {

	/**
	 * 选择排序
	 */
	public static void sort(Comparable[] comparables) {
		for (int c = 0; c <= comparables.length - 2; c++) {
			/**定义一个变量 假设第一个索引下 是最小值*/
			int minIndex = c;
			for (int i = c + 1; i < comparables.length; i++) {
				/**用最小的索引值 比较i的值*/
				if (greater(comparables[minIndex], comparables[i])) {
					minIndex = i;
				}
			}
			// 交换最小元素所在索引minIndex 所处的值和索引c所处的值
			exch(comparables, c, minIndex);

		}
	}

	public static boolean greater(Comparable v, Comparable w) {
		return v.compareTo(w) > 0;
	}

	public static void sortSelect(int[] nums) {

		for (int i = 0; i < nums.length - 1; i++) {
			int temp = i;
			for (int j = i + 1; j < nums.length; j++) {
				if (nums[temp] > nums[j]) {
					temp = j;
				}
			}
			int tt = nums[i];
			nums[i] = nums[temp];
			nums[temp] = tt;
		}

	}

	public static void main(String[] args) {
		int[] nums = { 1, 2, 5, 7, 11, 4, 2 };
		sortSelect(nums);
		System.out.println(Arrays.toString(nums));
	}

	public static void exch(Comparable[] a, int i, int j) {
		Comparable temp;
		temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
}
